package com.cskaoyan.service;

import com.cskaoyan.bean.category.*;
import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author oushao
 * @since  2022-06-04  23:38
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
     *
     * @return 查询所以商品类目的数据
     */
    @Override
    public CategoryData queryList() {


        //查找一级类目的数据
        List<Category> categories = categoryMapper.query();


        Iterator<Category> iterator = categories.iterator();

        while (iterator.hasNext()){
            Category next = iterator.next();
            int id = next.getId();
            //查找二级类目的数据
            List<Category2> categories2 = categoryMapper.query2(id);
            next.setChildren(categories2);
        }

        CategoryData categoryData = new CategoryData();
        categoryData.setList(categories);


        return categoryData;
    }

    /**
     * oushao  2022-06-06  10：39
     * 修改了MarketCategoryMapper.xml的insert方法，增加了获取自增的主键
     * @param marketCategory
     * @return
     */
    @Transactional
    @Override
    public MarketCategory insert(MarketCategory marketCategory) {

        Date date = new Date();

        marketCategory.setAddTime(date);
        marketCategory.setDeleted(false);

        marketCategoryMapper.insert(marketCategory);

        return marketCategory;
    }
    @Transactional
    @Override
    public void update(MarketCategory marketCategory) {

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(marketCategory.getId());
        Date date = new Date();
        marketCategory.setUpdateTime(date);

        marketCategoryMapper.updateByExample(marketCategory,marketCategoryExample);



    }

    @Override
    public CategoryL1Date queryLevel1() {

        PageHelper.startPage(1,Integer.MAX_VALUE);

        String s = "L1";

        Boolean deleted = new Boolean(false);

        List<CategoryDateFromLeve1> categoryDateFromLeve1 = (List<CategoryDateFromLeve1>) categoryMapper.queryLevel1(s,deleted);

        PageInfo pageInfo = new PageInfo(categoryDateFromLeve1);

        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        CategoryL1Date categoryL1Date = new CategoryL1Date();
        categoryL1Date.setPage(1);
        categoryL1Date.setLimit(10);
        categoryL1Date.setPages(pages);
        categoryL1Date.setTotal((int) total);
        categoryL1Date.setList(categoryDateFromLeve1);

        return categoryL1Date;
    }

    @Transactional
    @Override
    public void delete(MarketCategory marketCategory) {

        marketCategory.setDeleted(true);
        Date date = new Date();
        marketCategory.setUpdateTime(date);

        marketCategoryMapper.updateByPrimaryKey(marketCategory);

    }
}
